package cn.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

public class jsoupDemo2 {
    public static void main(String[] args) throws IOException {
        String path = jsoupDemo2.class.getClassLoader().getResource("student.xml").getPath();
        //获取html路径
        Document document = Jsoup.parse(new File(path), "UTF-8");
        //将html文件进行解析
        Elements students = document.getElementsByTag("student");
        //通过getElementsBytag获取所有学生对象
        Elements elements = document.getElementsByAttributeValue("number", "itcast_0001");
        //通过document对象的getElementByAttributeValue方法获取属性为number,属性值为itcast_0001的Elements对象
        System.out.println(elements);
        System.out.println("-------------------");
        Element student = document.getElementsByTag("student").get(0);
        //获取索引为一的学生对象
        Elements ele_name = student.getElementsByTag("name");
        //获取索引为一的学生对象的name属性
        System.out.println(ele_name);//输出属性
        System.out.println("-----------------");//分隔符
        String number = student.attr("number");
        //获取该学生属性为number的值
        System.out.println(number);
        System.out.println("-----------------");
        String text_name = ele_name.text();
        //将属性转化为文本信息
        String html_name = ele_name.html();
        //获取属性文本信息
        System.out.println(text_name);
        System.out.println(html_name);


    }
}
